from nicegui import ui
import pandas as pd
import plotly.graph_objects as go

class MyApp:
    def __init__(self):
        self.df = pd.DataFrame(columns=['x', 'y', 'z'])  # 将DataFrame作为实例变量
        self.setup_ui()

    def setup_ui(self):
        ui.label('登录系统')
        self.x = ui.input(label='x:')  # 将输入框作为实例变量
        self.y = ui.input(label='y:')
        self.z = ui.input(label='z:')

        fig = go.Figure()
        fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
        self.plot = ui.plotly(fig).classes('w-full h-40')  # 将plot作为实例变量
        ui.button('Add trace', on_click=self.add_trace)

    def add_trace(self, event=None):  # 添加event参数以兼容NiceGUI的回调机制
        fig = self.plot.figure
        fig.add_trace(go.Scatter(x=[1, 2], y=[self.x.value, self.y.value], mode="markers"))
        self.plot.update()

# 实例化MyApp类，启动UI
app = MyApp()
ui.run()
